//主页
var common = require('../../utils/common.js');
const app = getApp(); //获取全局app.js

//当前页面
Page({
	//页面使用的数据
	data: {
		appTitle: '黄河科技学院财务处', //小程序标题
		imageUrl: '/static/images/share.png', //分享封面图
		indicatorDots: true, //轮播图底部圆点
		autoplay: true, //轮播图自动播放
		interval: 3000, //轮播图切换间隔
		duration: 500, //轮播图切换动画时间
		slideImg: [], //幻灯片广告数据
		notice: [], //公告数据
		apartmentSuppliesShow: false, // 是否显示公寓用品
		apartmentMgrQueryShow: false, // 是否显示发放员公寓用品发放查询 
		apartmentCollegeQueryShow: false, // 是否显示学院负责人公寓用品发放查询
		bookAplyShow: false, // 是否显示教材发放申请
		bookDrawShow: false, // 是否显示教材个人发放
		bookDrawQueryShow: false, // 是否显示每日发放情况统计
		bookDrawConfirmShow: false, // 是否显示教材签收确认
		bookStatisticsShow: false, // 是否显示教材发放情况统计
		changeMajorShow: false, // 是否显示转专业学生
		wrongStudentShow: false, // 是否显示教材异常信息
	},

	//页面加载处理
	onLoad: function(e) {
		this.slideImg(); //获取幻灯片广告数据
	},

	//页面显示
	onShow: function() {
		var userToken = app.db.get('userToken');
		if (!userToken) {
			//app.common.jumpToLogin();
			// 初始化按钮状态
			this.setData({
				apartmentSuppliesShow: false,
				apartmentMgrQueryShow: false,
				apartmentCollegeQueryShow: false,
				bookAplyShow: false,
				bookDrawShow: false,
				bookDrawQueryShow: false,
				bookDrawConfirmShow: false,
				bookStatisticsShow: false,
				changeMajorShow: false,
				wrongStudentShow: false,
			});
		} else {
			this.menuInfo(); //获取用户等级
		}

		this.notice(); //获取公告数据 
	},

	//获取幻灯片广告数据
	slideImg: function() {
		var page = this;
		//异步获取幻灯片广告数据
		app.api.adList('tpl1_slider', function(res) {
			page.setData({
				slideImg: res.data.list
			});
		});
	},

	//获取公告数据
	notice: function() {
		var page = this;
		//异步获取公告数据，因为公告要求有实时性，所以不缓存
		app.api.noticeList(function(res) {
			if (res.status) {
				if (res.data) {
					page.setData({
						notice: res.data
					});
				}
			}
		});
	},

	//获取用户等级
	menuInfo: function() {
		var page = this;

		app.api.menuInfo(function(res) {
			var menus = JSON.parse(JSON.stringify(res));
			// 解析权限树
			for (var i = 0; i < menus.length; i++) {
				if (menus[i].code == 'Apartment') { // 公寓用品领用， 如果是跳转到二级页面的话这里需要做入口处理
					var subMenus = JSON.parse(JSON.stringify(menus[i].children))

					for (var j = 0; j < subMenus.length; j++) {
						if (subMenus[j].code == 'supplies') {
							if(page.data.apartmentSuppliesShow == false) {
								page.setData({
									apartmentSuppliesShow: true
								});
							}
						}
						
						if (subMenus[j].code == 'query') {
							if(page.data.apartmentQueryShow == false) {
								page.setData({
									apartmentQueryShow: true
								});
							}
						}
						if (subMenus[j].code == 'mgrquery') {
							if(page.data.apartmentMgrQueryShow == false) {
								page.setData({
									apartmentMgrQueryShow: true
								});
							}
						}
						if (subMenus[j].code == 'collegequery') {
							if(page.data.apartmentCollegeQueryShow == false) {
								page.setData({
									apartmentCollegeQueryShow: true
								});
							}
						}
						
					}
				} else if (menus[i].code == 'Book') { // 公寓用品领用， 如果是跳转到二级页面的话这里需要做入口处理
					var subMenus = JSON.parse(JSON.stringify(menus[i].children))

					for (var j = 0; j < subMenus.length; j++) {
						if (subMenus[j].code == 'bookaply') {
							if(page.data.bookAplyShow == false) {
								page.setData({
									bookAplyShow: true
								});
							}
						}
						if (subMenus[j].code == 'bookdraw') {
							if(page.data.bookDrawShow == false) {
								page.setData({
									bookDrawShow: true
								});
							}
						}
						if (subMenus[j].code == 'bookdrawquery') {
							if(page.data.bookDrawQueryShow == false) {
								page.setData({
									bookDrawQueryShow: true
								});
							}
						}
						if (subMenus[j].code == 'bookdrawconfirm') {
							if(page.data.bookDrawConfirmShow == false) {
								page.setData({
									bookDrawConfirmShow: true
								});
							}
						}
						if (subMenus[j].code == 'bookstatistics') {
							if(page.data.bookStatisticsShow == false) {
								page.setData({
									bookStatisticsShow: true
								});
							}
						}
					}
				} else if (menus[i].code == 'Student') { // 公寓用品领用， 如果是跳转到二级页面的话这里需要做入口处理
					var subMenus = JSON.parse(JSON.stringify(menus[i].children))

					for (var j = 0; j < subMenus.length; j++) {
						if (subMenus[j].code == 'changemajor') {
							if(page.data.changeMajorShow == false) {
								page.setData({
									changeMajorShow: true
								});
							}
						}
						if (subMenus[j].code == 'wrongstudent') {
							if(page.data.wrongStudentShow == false) {
								page.setData({
									wrongStudentShow: true
								});
							}
						}
					}
				}
			}
		});
	},

	//跳转到公告详情
	goNotice: function(e) {
		console.log(e.target.dataset.id);
		wx.navigateTo({
			url: '../notice/notice?id=' + e.target.dataset.id
		});
	},

	//前往公寓用品
	ApartmentSupplies: function() {
		wx.navigateTo({
			url: '/pages/apartment/Supplies/Supplies'
		});
	},
  
	//前往发放员公寓用品查询
	ApartmentMgrQuery: function() {
		wx.navigateTo({
			url: '/pages/apartment/MgrQueryEx/MgrQueryEx'
		});
	},

	//前往学院负责人公寓用品查询
	ApartmentCollegeQuery: function() {
		wx.navigateTo({
			url: '/pages/apartment/CollegeQuery/CollegeQuery'
		});
	},
	
	//前往教材发放申请
	BookAply: function() {
		wx.navigateTo({
			url: '/pages/book/BookAplyEx/BookAplyEx'
		});
	},
	
	//前往教材个人发放
	BookDraw: function() {
		wx.navigateTo({
			url: '/pages/book/BookDraw/BookDraw'
		});
	},
	
	//前往教材每日对账
	BookDrawQuery: function() {
		wx.navigateTo({
			url: '/pages/book/BookDrawQuery/BookDrawQuery'
		});
	},
	
	//教材签收确认
	BookDrawConfirm: function() {
		wx.navigateTo({
			url: '/pages/book/BookDrawConfirm/BookDrawConfirm'
		});
	},
	
	//前往教材发放统计
	BookStatistics: function() {
		wx.navigateTo({
			url: '/pages/book/BookStatistics/BookStatistics'
		});
	},
	
	//转专业管理
	ChangeMajor: function() {
		wx.navigateTo({
			url: '/pages/student/ChangeMajorStudentList/ChangeMajorStudentList'
		});
	},
	
	//转专业管理
	WrongStudent: function() {
		wx.navigateTo({
			url: '/pages/student/WrongStudent/WrongStudent'
		});
	},
	
	//转发分享
	onShareAppMessage: function() {
		let page = this;
		let userToken = app.db.get('userToken');
		if (userToken) {
			let myInviteCode = app.db.get('myInviteCode');
			if (myInviteCode) {
				//缓存里面有邀请码
				let ins = encodeURIComponent('invite=' + myInviteCode);
				let path = '/pages/index/index?scene=' + ins;
				return {
					title: page.data.appTitle,
					path: path,
					imageUrl: page.data.imageUrl
				}
			} else {
				let path = '/pages/index/index';
				return {
					title: page.data.appTitle,
					path: path,
					imageUrl: page.data.imageUrl
				}
			}
		} else {
			//用户没有登录
			let path = '/pages/index/index';
			return {
				title: page.data.appTitle,
				path: path,
				imageUrl: page.data.imageUrl
			}
		}
	}
});
